<template>
  <div>
    <el-form ref="form" :model="form">
      <el-form-item
      title="参数与参数之间请用‘&’连接"
      :label="$t('SeniorAdd_image')"
      prop="imageData"
      :rules="[
        { required: true, message: $t('SeniorAdd_pleaseEnterImageName')}
      ]"
      >
        <el-input v-model.trim="form.imageData" :placeholder="$t('SeniorAdd_forexample')"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button style="background: #067FB5; color: #fff;" size="small" @click="onSubmit">{{this.item ? $t('SeniorAdd_createImage') : $t('SeniorAdd_pullImage')}}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import http from '@/utils/http'
export default {
  data () {
    return {
      form: {
        imageData: ''
      }
    }
  },
  props: ['item'],
  methods: {
    onSubmit () {
      // 判断参数
      if (this.form.imageData === '') {
        return this.$message({
          showClose: true,
          message: '镜像为空',
          type: 'error'
        })
      }
      // 显示转圈
      this.loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      // ajax异步请求
      let docker = ''
      if (!this.item) {
        docker = `curl --unix-socket /var/run/docker.sock -s -X POST "http:/v1.40/images/create?${this.form.imageData}"` // 拉取镜像
      } else {
        docker = `curl --unix-socket /var/run/docker.sock -s -X POST "http:/v1.40/commit?container=${this.item.Id}&${this.form.imageData}"` // 从容器新建镜像
      }
      http({
        url: '/docker',
        method: 'POST',
        timeout: 6000000000,
        data: {
          docker
        }
      }).then(res => {
        if (res.Code === 200) {
          this.loading.close()
          this.$message({
            showClose: true,
            message: '拉取/创建成功！',
            type: 'success'
          })
          this.$emit('Success')
        } else {
          return this.$message({
            showClose: true,
            message: '镜像拉取失败，请重试',
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
